package com.yfl.tingyu.controller;


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yfl.tingyu.pojo.Company;
import com.yfl.tingyu.service.impl.CompanyServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2020-05-29
 */
@Controller
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyServiceImpl companyService;

    @RequestMapping("/companyManager")
    public String companyManager(){

        return "companyManager";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String,Object> list(Integer page, Integer rows,@RequestParam  Map<String,Object> params){

        Map<String, Object> company = companyService.selectCompany(page, rows,params);

        return  company;
    }

    //修改状态
    @RequestMapping("/changeStatus")
    @ResponseBody
    public Boolean changeStatus(Integer cid,String status){

        System.out.println("params:参数："+cid+status);

        String sta="";
        if (status.equals("0")){
            sta="2";
        }
        if (status.equals("1")){
            sta="2";
        }
        if (status.equals("2")){
            sta="1";
        }


        Company company=new Company();
        company.setCid(cid);
        company.setStatus(sta);
        boolean result = companyService.updateById(company);
        return result;
    }


    //添加账号
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Boolean saveOrUpdate(Company company){

        //设置注册时间为当前时间
        //判断是否是新增操作
        if (company.getCid() == null) {
            company.setStarttime(LocalDateTime.now());
        }//
        boolean saveOrUpdate = companyService.saveOrUpdate(company);

        return saveOrUpdate;
    }

    //删除账号
    @RequestMapping("/deleteCompnay")
    @ResponseBody
    public Boolean deleteCompnay(Integer[] cids){

        QueryWrapper<Company> qw=new QueryWrapper<>();
        qw.in("cid",cids);
        boolean remove = companyService.remove(qw);
        return remove;
    }

}

